package ti.me.seek2chat.adapters

import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import ti.me.seek2chat.databinding.ItemContainerReceivedMessageBinding
import ti.me.seek2chat.databinding.ItemContainerSentMessageBinding
import ti.me.seek2chat.models.ChatMessage
import java.util.Date

/**
 * ChatAdapter 是一个 RecyclerView 适配器，用于显示聊天消息。
 * 它根据消息的发送者来区分显示发送和接收的消息。
 *
 * @param chatMessages 聊天消息列表
 * @param receiverProfileImages 接收者的头像
 * @param senderId 当前用户的 ID
 */
class ChatAdapter(
    private var chatMessages: ArrayList<ChatMessage>,
    private var receiverProfileImages: Bitmap,
    private var senderId: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val viewTypeSent = 1
    private val viewTypeReceived = 2

    /**
     * SentMessageViewHolder 用于显示发送的消息。
     *
     * @param binding 绑定的视图
     */
    class SentMessageViewHolder(private val binding: ItemContainerSentMessageBinding) :
        RecyclerView.ViewHolder(binding.root) {
        /**
         * 设置发送消息的数据。
         *
         * @param message 消息内容
         */
        fun setData(message: String) {
            binding.textMessage.text = message
            binding.textDateTime.text = Date().toString()
        }
    }

    /**
     * ReceivedMessageViewHolder 用于显示接收的消息。
     *
     * @param binding 绑定的视图
     */
    class ReceivedMessageViewHolder(private val binding: ItemContainerReceivedMessageBinding) :
        RecyclerView.ViewHolder(binding.root) {
        /**
         * 设置接收消息的数据。
         *
         * @param message 消息内容
         * @param receiverProfileImages 接收者的头像
         */
        fun setData(message: String, receiverProfileImages: Bitmap) {
            binding.textMessage.text = message
            binding.textDateTime.text = Date().toString()
            binding.imageProfile.setImageBitmap(receiverProfileImages)
        }
    }

    /**
     * 根据消息的发送者返回不同的视图类型。
     *
     * @param position 消息的位置
     * @return 视图类型
     */
    override fun getItemViewType(position: Int): Int {
        return if (chatMessages[position].senderId == senderId) {
            viewTypeSent
        } else {
            viewTypeReceived
        }
    }

    /**
     * 创建 ViewHolder。
     *
     * @param parent 父视图
     * @param viewType 视图类型
     * @return 创建的 ViewHolder
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == viewTypeSent) {
            return SentMessageViewHolder(
                ItemContainerSentMessageBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            return ReceivedMessageViewHolder(
                ItemContainerReceivedMessageBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    /**
     * 绑定 ViewHolder。
     *
     * @param holder ViewHolder
     * @param position 消息的位置
     */
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == viewTypeSent) {
            (holder as SentMessageViewHolder).setData(chatMessages[position].message)
        } else {
            (holder as ReceivedMessageViewHolder).setData(
                chatMessages[position].message,
                receiverProfileImages
            )
        }
    }

    /**
     * 返回消息的数量。
     *
     * @return 消息数量
     */
    override fun getItemCount(): Int {
        return chatMessages.size
    }
}